﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;

/// <summary>
/// Summary description for ResizeImage
/// </summary>
public class ResizeImage
{
    public  int IMAGE_SW = 113;
    public  int IMAGE_SH = 85;
    public int IMAGE_MW = 1024;
    public  int IMAGE_MH = 768;
    public int IMAGE_LW = 1280;
    public int IMAGE_LH = 1024;
    public ResizeImage()
    {
       
    }
    public ResizeImage(int smallWidth, int smallHeight, int mediumWidth, int mediumHeight,int largeWidth, int largeHeight)
    {
        IMAGE_SW = smallWidth;
        IMAGE_SH = smallHeight;
        IMAGE_MW = mediumWidth;
        IMAGE_MH = mediumHeight;
        IMAGE_LW = largeWidth;
        IMAGE_LH = largeHeight;
    }

    /// <summary>
    /// Resize image
    /// </summary>
    /// <param name="imageSourcePath">Duong dan file hinh goc</param>
    /// <param name="imageDestPath">Thu muc chua file nhan</param>
    /// <param name="type">1:resize nho; 2:resize trung binh, 3:resize lon</param>
    /// <returns></returns>
    public Boolean Resize(string imageSourcePath, string imageDestPathFolder,string filename, int type)
    {
        try
        {
            Image imgSource = Image.FromFile(imageSourcePath);
            string imageDestPath = imageDestPathFolder + "/" + filename;
            if (File.Exists(imageDestPath))
            {
                File.Delete(imageDestPath);
            }
            //get the height and width of the image
            double originalW = (double)imgSource.Width;
            double originalH = (double)imgSource.Height;
            double scale = originalW / originalH;
            double dScale = 0;
            //get the new size based on the percentage change
            int resizedW = 0;
            int resizedH = 0;
            switch (type)
            {
                case 1:
                    dScale = (double)IMAGE_SW / (double)IMAGE_SH;
                    break;
                case 2:
                    dScale = (double)IMAGE_MW / (double)IMAGE_MH;
                    break;
                case 3:
                    dScale = (double)IMAGE_LW / (double)IMAGE_LH;
                    break;
            }
            if (scale >= 1 && dScale < 1)
            {
                switch (type)
                {
                    case 1:
                        resizedW = IMAGE_SW;
                        break;
                    case 2:
                        resizedW = IMAGE_MW;
                        break;
                    case 3:
                        resizedW = IMAGE_LW;                        
                        break;
                }
                resizedH = (int)((double)resizedW * scale);
            }
            else if (scale < 1 && dScale >= 1)
            {
                switch (type)
                {
                    case 1:
                        resizedH = IMAGE_SH;
                        break;
                    case 2:
                        resizedH = IMAGE_MH;
                        break;
                    case 3:
                        resizedH = IMAGE_LH;
                        break;
                }
                resizedW = (int)((double)resizedH * scale);
            }
            else
            {
                switch (type)
                {
                    case 1:
                        resizedW = IMAGE_SW;
                        resizedH = IMAGE_SH;
                        break;
                    case 2:
                        resizedW = IMAGE_MW;
                        resizedH = IMAGE_MH;
                        break;
                    case 3:
                        resizedW = IMAGE_LW;
                        resizedH = IMAGE_LH;
                        break;
                }
            }

            //create a new Bitmap the size of the new image
            Bitmap bmp = new Bitmap(resizedW, resizedH);
            //create a new graphic from the Bitmap
            Graphics graphic = Graphics.FromImage((Image)bmp);
            graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
            //draw the newly resized image
            graphic.DrawImage(imgSource, 0, 0, resizedW, resizedH);
            //dispose and free up the resources
            graphic.Dispose();
            //return the image
            ((Image)bmp).Save(imageDestPath);
            return true;
        }
        catch
        {
            return false;
        }

    }
}
